//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FormTesterReportsViewer.h"
#include "FormSendTesterReport.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DBCtrlsEh"
#pragma link "DBGridEh"
#pragma link "DBGridEhGrouping"
#pragma link "DBLookupEh"
#pragma link "FIBDatabase"
#pragma link "FIBDataSet"
#pragma link "GridsEh"
#pragma link "pFIBDatabase"
#pragma link "pFIBDataSet"
#pragma link "PropFilerEh"
#pragma link "PropStorageEh"
#pragma link "SIBEABase"
#pragma link "SIBFIBEA"
#pragma link "trayicon"
#pragma resource "*.dfm"
TTesterReportsViewerForm *TesterReportsViewerForm;
//---------------------------------------------------------------------------

__fastcall TTesterReportsViewerForm::TTesterReportsViewerForm(TComponent* Owner)
	: TForm(Owner)
{
	PropStorageEh->Section = this->Name;
	Constraints->MinWidth = Width;
	Constraints->MinHeight = Height;
	TesterUpdatesCount = DeveloperUpdatesCount = 0;
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::FormShow(TObject *Sender)
{
	EventAlerter->Events->Clear();
	EventAlerter->Events->Add("TEST_REPORTS_UPDATED_DEVELOPER");
	EventAlerter->Events->Add("TEST_REPORTS_UPDATED_TESTER");
	EventAlerter->RegisterEvents();
	DataSet->Open();
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::FormHide(TObject *Sender)
{
	if(ReadTransaction->Active) ReadTransaction->Rollback();
	if(WriteTransaction->Active) WriteTransaction->Rollback();
	if(EventAlerter->Registered) EventAlerter->UnRegisterEvents();
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::DBGridEh1Columns3GetCellParams(TObject *Sender,
		  bool EditMode, TColCellParamsEh *Params)
{
	switch(DataSet->FBN("CATEGORY_ID")->AsInteger) {
		case 0:
			Params->Background = TColor(0xB0B0FF);
			break;
		case 1:
			Params->Background = TColor(0xA0D0FF);
			break;
		case 2:
			Params->Background = TColor(0xB0FFFF);
			break;
		default:
			Params->Background = DBGridEh->Color;
	}
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::DBGridEhDblClick(TObject *Sender)
{
	SendTesterReportForm->EditReportID = DataSet->FBN("ID")->AsInteger;
	SendTesterReportForm->EditReportText = DataSet->FBN("TEXT")->AsString;
	EventAlerter->UnRegisterEvents();
	SendTesterReportForm->ShowModal();
	EventAlerter->RegisterEvents();
	DataSet->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::EventAlerterEventAlert(TObject *Sender, UnicodeString EventName, int EventCount)
{
	if(EventName == "TEST_REPORTS_UPDATED_TESTER")
		TesterUpdatesCount++;
	else if(EventName == "TEST_REPORTS_UPDATED_DEVELOPER")
		DeveloperUpdatesCount++;
	AllUpdatesCountPanel->Caption = String(TesterUpdatesCount + DeveloperUpdatesCount);
	DeveloperUpdatesCountPanel->Caption = String(DeveloperUpdatesCount);
	StatusPanel->Font->Color = TColor(clWindowText);
	TrayIcon->IconIndex = 2;
	TrayIcon->ShowBalloonHint();
	Timer->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::TimerTimer(TObject *Sender)
{
	TrayIcon->IconIndex = 1;
	Timer->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::TrayIconBalloonClick(TObject *Sender)
{
	TrayIcon->IconIndex = 1;
	Show();
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::RefreshButtonClick(TObject *Sender)
{
	DataSet->FullRefresh();
	TesterUpdatesCount = DeveloperUpdatesCount = 0;
	AllUpdatesCountPanel->Caption = "0";
	DeveloperUpdatesCountPanel->Caption = "0";
	StatusPanel->Font->Color = TColor(clGray);
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::DBGridEhGetCellParams(TObject *Sender, TColumnEh *Column,
		  TFont *AFont, TColor &Background, TGridDrawState State)
{
	enum { colProcessed, colClosed, colOther } Col;
	Col = (Column->FieldName == "IS_PROCESSED") ? colProcessed : (Column->FieldName == "IS_CLOSED") ? colClosed : colOther;
	bool IsProcessed = DataSet->FBN("IS_PROCESSED")->AsInteger;
	bool IsClosed = DataSet->FBN("IS_CLOSED")->AsInteger;
	String Role = DataSet->Database->ConnectParams->RoleName.UpperCase();
	String User = DataSet->Database->ConnectParams->UserName.UpperCase();
	String RepUser = DataSet->FBN("DB_USER_NAME")->AsString;

	if((Col == colProcessed && (Role != "DEVELOPER" || IsClosed) && User != RepUser) ||
	(Col == colClosed && User != RepUser))
		Background = clBtnFace;
}
//---------------------------------------------------------------------------

void __fastcall TTesterReportsViewerForm::DBGridEhCellClick(TColumnEh *Column)
{
	enum { colProcessed, colClosed, colOther } Col;
	Col = (Column->FieldName == "IS_PROCESSED") ? colProcessed : (Column->FieldName == "IS_CLOSED") ? colClosed : colOther;
	bool IsProcessed = DataSet->FBN("IS_PROCESSED")->AsInteger;
	bool IsClosed = DataSet->FBN("IS_CLOSED")->AsInteger;
	String Role = DataSet->Database->ConnectParams->RoleName.UpperCase();
	String User = DataSet->Database->ConnectParams->UserName.UpperCase();
	String RepUser = DataSet->FBN("DB_USER_NAME")->AsString;

	if(Col == colProcessed && ((Role == "DEVELOPER" && !IsClosed) || User == RepUser)) {
		DataSet->Edit();
		DataSet->FBN("IS_PROCESSED")->AsInteger = !IsProcessed;//DataSet->FBN("IS_PROCESSED")->AsInteger;
		EventAlerter->UnRegisterEvents();
		DataSet->Post();
		EventAlerter->RegisterEvents();
	}
	else if(Col == colClosed && User == RepUser) {
		DataSet->Edit();
		DataSet->FBN("IS_CLOSED")->AsInteger = !IsClosed;//DataSet->FBN("IS_CLOSED")->AsInteger;
		EventAlerter->UnRegisterEvents();
		DataSet->Post();
		EventAlerter->RegisterEvents();
	}
}
//---------------------------------------------------------------------------

